using System;
using System.Collections.Generic;
using System.Text;

using Entities;

namespace Bll
{
    public class CartBll
    {
        // Internal storage for a cart	  
        private Dictionary<string, CartItemInfo> cartItems = new Dictionary<string, CartItemInfo>();

        /// <summary>
        /// Calculate the total for all the cartItems in the Cart
        /// </summary>
        public decimal Total
        {
            get
            {
                decimal total = 0;
                foreach (CartItemInfo item in cartItems.Values)
                    total += item.Price * item.Quantity;
                return total;
            }
        }

        /// <summary>
        /// Update the quantity for item that exists in the cart
        /// </summary>
        /// <param name="itemId">Item Id</param>
        /// <param name="qty">Quantity</param>
        public void SetQuantity(string itemId, int qty)
        {
            if (qty == 0)
            {
                Remove(itemId);
            }
            else
            {
                cartItems[itemId].Quantity = qty;
            }
        }

        /// <summary>
        /// Return the number of unique items in cart
        /// </summary>
        public int Count
        {
            get { return cartItems.Count; }
        }

        /// <summary>
        /// Add an item to the cart.
        /// When ItemId to be added has already existed, this method will update the quantity instead.
        /// </summary>
        /// <param name="itemId">Item Id of item to add</param>
        public void Add(string itemId)
        {
            CartItemInfo cartItem;
            if (!cartItems.TryGetValue(itemId, out cartItem))
            {
                ProductBll item = new ProductBll();
                ProductInfo data = item.GetByID(itemId);
                if (data != null)
                {
                    CartItemInfo newItem = new CartItemInfo(itemId, data.ProductName, 1, (decimal)data.UnitPrice, data.DescriptionBrief, data.UnitsInStock, data.Picture, data.CategoryID, data.ProductID);
                    cartItems.Add(itemId, newItem);
                }
            }
            else
                cartItem.Quantity++;
        }

        /// <summary>
        /// Add an item to the cart.
        /// When ItemId to be added has already existed, this method will update the quantity instead.
        /// </summary>
        /// <param name="item">Item to add</param>
        public void Add(CartItemInfo item)
        {
            CartItemInfo cartItem;
            if (!cartItems.TryGetValue(item.ItemId, out cartItem))
                cartItems.Add(item.ItemId, item);
            else
                cartItem.Quantity += item.Quantity;
        }

        /// <summary>
        /// Remove item from the cart based on itemId
        /// </summary>
        /// <param name="itemId">ItemId of item to remove</param>
        public void Remove(string itemId)
        {
            cartItems.Remove(itemId);            
        }

        /// <summary>
        /// Returns all items in the cart. Useful for looping through the cart.
        /// </summary>
        /// <returns>Collection of CartItemInfo</returns>
        public ICollection<CartItemInfo> CartItems
        {
            get { return cartItems.Values; }
        }

        /// <summary>
        /// Method to convert all cart items to order line items
        /// </summary>
        /// <returns>A new array of order line items</returns>
        public LineItemInfo[] GetOrderLineItems()
        {

            LineItemInfo[] orderLineItems = new LineItemInfo[cartItems.Count];
            int lineNum = 0;
            foreach (CartItemInfo item in cartItems.Values)
                orderLineItems[lineNum] = new LineItemInfo(++lineNum, item.Picture, item.ItemId, item.Name, item.Price, item.Quantity, item.UnitInStock);

            return orderLineItems;
        }

        public string ValidateQuantity(string quantity)
        {
            string ok = null;
            char[] arr = quantity.ToCharArray();
            for (int i = 0; i < arr.Length; i++)
            {
                if (char.IsDigit(arr[i]))
                {
                    if (arr.Length < 99)
                    {
                        ok = "ok";
                        //break;
                    }
                    else
                    {
                        ok = "out_of_range";
                        //break;
                    }
                }
                else
                {
                    ok = null;
                }
            }
            return ok;
        }

        /// <summary>
        /// Clear the cart
        /// </summary>
        public void Clear()
        {
            cartItems.Clear();
        }     
    }
}
